package com.roncoo.education.course.service.auth.biz;

import java.util.*;
import java.util.stream.Collectors;

import javax.validation.constraints.NotNull;

import org.springframework.stereotype.Component;

import com.alibaba.csp.sentinel.util.StringUtil;
import com.roncoo.education.common.core.base.Page;
import com.roncoo.education.common.core.base.PageUtil;
import com.roncoo.education.common.core.base.Result;
import com.roncoo.education.common.core.tools.BeanUtil;
import com.roncoo.education.common.service.BaseBiz;
import com.roncoo.education.course.dao.ExperimentScoreDao;
import com.roncoo.education.course.dao.ExperimentScoreReportDao;
import com.roncoo.education.course.dao.ExperimentScoreStepDao;
import com.roncoo.education.course.dao.impl.mapper.entity.Experiment;
import com.roncoo.education.course.dao.impl.mapper.entity.ExperimentExample;
import com.roncoo.education.course.dao.impl.mapper.entity.ExperimentScore;
import com.roncoo.education.course.dao.impl.mapper.entity.ExperimentScoreExample;
import com.roncoo.education.course.dao.impl.mapper.entity.ExperimentScoreExample.Criteria;
import com.roncoo.education.course.dao.impl.mapper.entity.ExperimentScoreReport;
import com.roncoo.education.course.dao.impl.mapper.entity.ExperimentScoreReportExample;
import com.roncoo.education.course.dao.impl.mapper.entity.ExperimentScoreStep;
import com.roncoo.education.course.dao.impl.mapper.entity.ExperimentScoreStepExample;
import com.roncoo.education.course.service.auth.req.AuthExperimentScoreReq;
import com.roncoo.education.course.service.auth.resp.AuthExperimentScoreResp;
import com.roncoo.education.course.service.auth.resp.AuthExperimentScoreStepResp;
import com.roncoo.education.course.service.biz.resp.ExperimentResp;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import lombok.RequiredArgsConstructor;

/**
 * AUTH-实验分数
 *
 * @author wujing
 */
@Component
@RequiredArgsConstructor
public class AuthExperimentScoreBiz extends BaseBiz {

    @NotNull
    private final ExperimentScoreDao dao;
    @NotNull
    private final ExperimentScoreStepDao stepDao;    
    @NotNull
    private final ExperimentScoreReportDao reportDao;

    public Result<Page<AuthExperimentScoreResp>> listForPage(AuthExperimentScoreReq req, long userId) {
        ExperimentScoreExample example = new ExperimentScoreExample();
        Criteria criteria = example.createCriteria();
        if (StringUtil.isNotBlank(req.getAppid())) { 
        	criteria.andAppidEqualTo(req.getAppid());	
        }
        if (StringUtil.isNotBlank(req.getUsername())) { 
        	criteria.andUsernameEqualTo(Long.toString(userId));
        }
        if (StringUtil.isNotBlank(req.getMobile())) {
        	criteria.andMobileLike("%" + req.getMobile() + "%");
        }
        if (StringUtil.isNotBlank(req.getTitle())) {
        	criteria.andTitleLike("%" + req.getTitle() + "%");
        }        
        example.setOrderByClause("start_time desc");
        Page<ExperimentScore> userExperimentPage = dao.page(req.getPageCurrent(), req.getPageSize(), example);
        Page<AuthExperimentScoreResp> respPage = PageUtil.transform(userExperimentPage, AuthExperimentScoreResp.class);                
        
        if (CollUtil.isNotEmpty(respPage.getList())) {
            List<Long> IdList = respPage.getList().stream().map(AuthExperimentScoreResp::getId).collect(Collectors.toList());
            // 用户学习记录，获取每个课程里面最新学习的课时
            ExperimentScoreReportExample example2 = new ExperimentScoreReportExample();
            com.roncoo.education.course.dao.impl.mapper.entity.ExperimentScoreReportExample.Criteria criteria2 = example2.createCriteria().andExperimentScoreIdIn(IdList);
            List<ExperimentScoreReport> reportList = reportDao.listByExample(example2);
            Map<Long, ExperimentScoreReport> map = new HashMap<>();
            if (CollUtil.isNotEmpty(reportList)) {
            	map = reportList.stream().collect(Collectors.toMap(item -> item.getExperimentScoreId(), item -> item));
            }

            for (AuthExperimentScoreResp resp : respPage.getList()) {
            	ExperimentScoreReport report = map.get(resp.getId());
                if (ObjectUtil.isNotEmpty(report)) {
                	resp.setReportId(report.getId());
//                	resp.setReportContents(report.getContents());
                }
            }
        }
        return Result.success(respPage);
    }
    
    public Result<List<AuthExperimentScoreStepResp>> listSteps(AuthExperimentScoreReq req) {
    	ExperimentScoreStepExample example = new ExperimentScoreStepExample();
        example.createCriteria().andExperimentScoreIdEqualTo(req.getId());
        example.setOrderByClause("start_time asc");
        List<ExperimentScoreStep> experimentList = stepDao.listByExample(example);
        List<AuthExperimentScoreStepResp> respList = PageUtil.copyList(experimentList, AuthExperimentScoreStepResp.class);

        return Result.success(respList);
    }    
    
    public ExperimentScoreReport getById(long reportId) {
    	return reportDao.getById(reportId);
    }
}
